package GUI;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import Game.GameHelper;
import Game.Objects.GOPlayer;
import Tools.SoundManager;
import Tools.Drawing.FontTagSystem;

// TODO: add BLK. stat (block chance)

/** GUI Skills class
 * @author Aikidos
 */
public class GuiSkills {

	/** GUI is open? */
	private boolean _isOpen;
	
	/** Player */
	private GOPlayer _player;
	
	/** Strength */
	private int _str;
	
	/** Con. */
	private int _con;
	
	/** Luck */
	private int _luck;
	
	/** Intelligence */
	private int _int;
	
	/** Icon 'plus' white */
	private Image _imagePlusWhite;
	
	/** Icon 'plus' red */
	private Image _imagePlusRed;
	
	/** Selected index */
	private int _selected;
	
	/** Maximum selected index */
	private int _selectedMax = 3;
	
	public GuiSkills(GOPlayer player) throws SlickException {
		_player = player;
		
		_imagePlusWhite = new Image("res/gfx/icons/plus_white.png");
		_imagePlusRed = new Image("res/gfx/icons/plus_red.png");
	}
	
	/** Returns true if GUI is open
	 * @return True if GUI is open
	 */
	public boolean isOpen() {
		return _isOpen;
	}
	
	/** Open GUI */
	public void open() {
		_isOpen = true;
		
		// Lock camera control
		_player.getObjectManager().getCamera().lock();
	}
	
	/** Close GUI */
	public void close() {
		_isOpen = false;
	}
	
	public void update(int delta) {
		if (_isOpen) {
			// Get input system
			Input input = GameHelper.getGame().getInput();
			
			// Select
			if (input.isKeyPressed(Input.KEY_DOWN)) {
				_selected++;
				if (_selected > _selectedMax)
					_selected = _selectedMax;
				
				// Play sound
				SoundManager.play("sfx_skill");
			}

			if (input.isKeyPressed(Input.KEY_UP)) {
				_selected--;
				if (_selected < 0)
					_selected = 0;
				
				// Play sound
				SoundManager.play("sfx_skill");
			}
			
			// Upgrade
			if (input.isKeyPressed(Input.KEY_RETURN)) {
				if (_player.getSkillPoints() > 0) {
					switch (_selected) {
					case 0:
						_str++;
						_player.MinDamage++;
						_player.MaxDamage++;
						break;
						
					case 1:
						_con++;
						_player.MaxWeight += 1;
						_player.MaxHealth += 5;
						break;
						
					case 2:
						_luck++;
						_player.addLuck(1);
						break;
						
					case 3:
						_int++;
						_player.addIntelligence(1);
						break;
					}
					
					_player.spendSkillPoint();
				}
			}
		}
	}
	
	public void draw(Graphics g) {
		if (_isOpen) {
			g.setColor(Color.black);
			g.fillRect(0, 100, GameHelper.getGame().getWidth(), 400);
			
			g.setColor(Color.white);
			g.drawLine(0, 99, GameHelper.getGame().getWidth(), 99);
			g.drawLine(0, 502, GameHelper.getGame().getWidth(), 502);
			
			//////////////////////////
			////////// Draw //////////
			//////////////////////////
			
			// Number of skill points
			if (_player.getSkillPoints() > 0)
				FontTagSystem.drawText("S. Points: [green]" + _player.getSkillPoints() + "[/green]", 
						new Vector2f(GameHelper.getGame().getWidth() - 20, 470), FontTagSystem.EAlign.RIGHT);
			else
				FontTagSystem.drawText("S. Points: [red]0[/red]", 
						new Vector2f(GameHelper.getGame().getWidth() - 20, 470), FontTagSystem.EAlign.RIGHT);
			
			// Skills
			FontTagSystem.drawText("STR.", new Vector2f(20, 120));
			FontTagSystem.drawText("[green]" + _str + "[/green]", new Vector2f(60, 120));
			
			FontTagSystem.drawText("CON.", new Vector2f(20, 150));
			FontTagSystem.drawText("[green]" + _con + "[/green]", new Vector2f(60, 150));
			
			FontTagSystem.drawText("LCK.", new Vector2f(20, 180));
			FontTagSystem.drawText("[green]" + _luck + "[/green]", new Vector2f(60, 180));
			
			FontTagSystem.drawText("INT.", new Vector2f(20, 210));
			FontTagSystem.drawText("[green]" + _int + "[/green]", new Vector2f(60, 210));
			
			/////////////////////////////////
			////////// Description //////////
			/////////////////////////////////
			FontTagSystem.drawText("Description:", new Vector2f(200, 120));
			
			// Set description
			String desc = "";
			switch (_selected) {
			case 0:
				desc = "Increase [green]Min.Damage[/green] and [green]Max.Damage[/green] by 1";
				break;
				
			case 1:
				desc = "Increase [green]Max.Health[/green] by 5 and [green]Max. Weight[/green] by 1";
				break;
				
			case 2:
				desc = "Increase chance of rare loot drops";
				break;
				
			case 3:
				desc = "Increase [green]EXP[/green] and [cyan]Magic[/cyan] bonus";
				break;
			}
			
			FontTagSystem.drawText(desc, new Vector2f(200, 150));
			
			// Plus
			if (_player.getSkillPoints() > 0) {
				Vector2f drawPos = new Vector2f(100, 123 + (_selected * 30));
				_imagePlusWhite.draw(drawPos.x, drawPos.y);
			}
			else {
				Vector2f drawPos = new Vector2f(100, 123 + (_selected * 30));
				_imagePlusRed.draw(drawPos.x, drawPos.y);
			}
		}
	}
	
}
